package io.zhengsh.juc._3queue;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;


@Slf4j
public class SynchronousQueueTest {

    final static BlockingQueue<Integer> blockingQueue = new SynchronousQueue<>();

    public static void main(String[] args) throws InterruptedException {
        new Thread(()-> take(), "c1").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()-> take(), "c2").start();

        TimeUnit.SECONDS.sleep(1);
        new Thread(()-> put(1), "p1").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()-> put(2), "p2").start();
    }

    private static void put(int i) {
        try {
            blockingQueue.put(i);
            log.info("put val: {}", i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void take() {
        try {
            Integer take = blockingQueue.take();
            log.info("take val: {}", take);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
